//
//  MyMessageViewController.m
//  JxCarios
//
//  Created by xcq on 15/3/1.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "MyMessageViewController.h"
#import "MyMessageTableViewCell.h"

#define INTCONVERSTR(value)   [NSString stringWithFormat:@"%d",value]

static NSString *myMessage = @"myMessage";
@interface MyMessageViewController ()

@end

@implementation MyMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initAbout];
    [self netRequest];
}
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    UIView *btn = (UIView *)[self.tabBarController.view  viewWithTag:400];
    btn.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
    UIView *btn = (UIView *)[self.tabBarController.view  viewWithTag:400];
    btn.hidden = NO;
}


- (void)initAbout {
    self.title = @"我的消息";
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:nil andCustom:NO];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyMessageTableViewCell" bundle:nil] forCellReuseIdentifier:myMessage];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
    self.tableView.tableFooterView = [UIView new];
}

- (void)footerRefresh {
    
    if (_totalPage <= INTCONVERSTR(_currenPage)) {
        [self.view makeToast:@"没有更多数据了哦"];
        [_tableView footerEndRefreshing];
        return;
    }
    _currenPage++;
     _carAround.page = INTCONVERSTR(_currenPage);
    [_carAround getMyMessage:^(id result) {
        [_array addObjectsFromArray:[result customObjectForkey:@"msglist"]];

        _totalPage = [result customObjectForkey:@"totalpages"];
        [self initDataSource];
        [self.tableView footerEndRefreshing];

    } anfFail:^(id result) {
        _currenPage --;
        [self.tableView footerEndRefreshing];
    }];
    
}

- (void)netRequest {
    if (!_carAround) {
        _carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    
    _currenPage = 1;
    _carAround.page = [NSString stringWithFormat:@"%d",1];
    [_carAround getMyMessage:^(id result) {
        _array = [NSMutableArray arrayWithArray:[result customObjectForkey:@"msglist"]];
        [self initDataSource];
     } anfFail:^(id result) {
        

    }];
     
}

- (void)initDataSource {
    if (_array.count <= 0) {
        return;
    }
    if (!self.tableView.delegate) {
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
    }
 
    [self.tableView reloadData];
}

#pragma mark - tableView delegate & dateSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count == 0 ? 0 : _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myMessage forIndexPath:indexPath];
    cell.oilCardLabel.text = [[_array objectAtIndex:indexPath.row] customObjectForkey:@"CREATE_DATE"];
    cell.detailContent.text = [[_array objectAtIndex:indexPath.row] customObjectForkey:@"CONTENT"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 88;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


@end
